//package com.liulimi.fastapi.config;
//
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.http.HttpMethod;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder;
//import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
//import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
//import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
//import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
//import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
//import org.springframework.security.oauth2.provider.ClientDetailsService;
//import org.springframework.security.oauth2.provider.client.InMemoryClientDetailsService;
//import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
//import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
//import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;
//import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
//import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
//import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
//import org.springframework.security.oauth2.provider.token.TokenStore;
//import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
//import org.springframework.stereotype.Component;
//
//import javax.sql.DataSource;
//
//@Configuration
////@EnableAuthorizationServer
//public class AuthorizationService extends AuthorizationServerConfigurerAdapter {
//
//    //客户端详情服务
//    @Override
//    public void configure(ClientDetailsServiceConfigurer clients)
//            throws Exception {
//
//        clients.withClientDetails(clientDetailsService1());
//
//
////        clients.withClientDetails(clientDetailsService);
//        ;
//    }
//
//    @Bean
//    public TokenStore tokenStore() {
//        return new InMemoryTokenStore();
//    }
//
//    @Autowired
//    private TokenStore tokenStore;
//    @Autowired
//    private AuthenticationManager authenticationManager;
//    @Autowired
//    private AuthorizationCodeServices authorizationCodeServices;
//    @Autowired
//    private AuthorizationServerTokenServices authorizationServerTokenServices;
//
//    @Bean
//    public AuthorizationServerTokenServices tokenService() throws Exception {
//        DefaultTokenServices service = new DefaultTokenServices();
//        service.setClientDetailsService(clientDetailsService1());
//        service.setSupportRefreshToken(true);
//        service.setTokenStore(tokenStore);
//        service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时
//        service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天
//        return service;
//    }
//
//    @Bean
//    public AuthorizationCodeServices authorizationCodeServices() {
//        return new InMemoryAuthorizationCodeServices();//设置授权码模式的授权码如何存取
//    }
//
//    @Override
//    public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
//        endpoints
//                .authenticationManager(authenticationManager)//认证管理器
//                .authorizationCodeServices(authorizationCodeServices)//授权码服务
//                .tokenServices(authorizationServerTokenServices)//令牌管理服务
//                .allowedTokenEndpointRequestMethods(HttpMethod.POST);
//    }
//
//    @Override
//    public void configure(AuthorizationServerSecurityConfigurer security) {
//        security
//                .tokenKeyAccess("permitAll()")                    //oauth/token_key是公开
//                .checkTokenAccess("permitAll()")                  //oauth/check_token公开
//                .allowFormAuthenticationForClients()                //表单认证（申请令牌）
//        ;
//    }
//
//
//    //将客户端信息存储到数据库
//
//    public static ClientDetailsService clientDetailsService1() throws Exception {
//        InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = new InMemoryClientDetailsServiceBuilder();
//        // @formatter:off
//        inMemoryClientDetailsServiceBuilder
//                .withClient("client1")
//                .secret("123456")
//                .resourceIds("resource1")
//                .authorizedGrantTypes("authorization_code", "password",
//                        "client_credentials", "implicit", "refresh_token")
//                .scopes("all")
//                .autoApprove(false)
//                .redirectUris("http://www.baidu.com");
//        return inMemoryClientDetailsServiceBuilder.build();
//
//    }
//
//
//}
